using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;

using Janus.Windows.TimeLine;
namespace Janus.NorthwindDemo2007
{
	public partial class TimelineView
	{

		protected override void OnDataProviderChanged()
		{
			this.Timeline.DataSource = this.DataSource;
		}
		public override FolderView[] GetDefinedViews()
		{
			//TODO: Load the layout file to include the layouts saved in there 
			//that are not present in the layout
			ArrayList list = new ArrayList();
			AddViews(this.Timeline.Layouts, list);
			FolderView[] views = new FolderView[list.Count];

			for (int i = 0; i < list.Count; i++)
			{
				TimeLineLayout layout = (TimeLineLayout)(list[i]);
				views[i] = new FolderView(layout.Description, ContentViewType.TimelineView, layout.Key);
			}
			return views;
		}
		private void AddViews(TimeLineLayoutCollection layouts, ArrayList list)
		{
			foreach (TimeLineLayout layout in layouts)
			{
				if (layout.Tag != "NOSHOW")
				{
					list.Add(layout);
				}
				AddViews(layout.ChildLayouts, list);
			}
		}
		public override int ItemCount
		{
			get
			{
				return this.Timeline.Items.Count;
			}
		}

		public void ShowGroupByDialog()
		{
			TimeLineGroupByDialog frm = new TimeLineGroupByDialog();
			frm.ShowDialog(this.Timeline);
			frm.Dispose();
		}
		public void ShowFormattingDialog()
		{
			FormatsForm frm = new FormatsForm();
			frm.ShowDialog(this.Timeline);
			frm.Dispose();
		}
		public void ShowTimescaleDialog()
		{
			TimescaleForm frm = new TimescaleForm();
			frm.ShowTimescaleFormDialog(this.Timeline);
		}

        public override Janus.Windows.Ribbon.Ribbon RibbonControl
        {
            get
            {
                return this.Ribbon1;
            }
        }

		public override void PrintPreview(Form ownerForm)
		{
			frmPrintPreview preview = new frmPrintPreview();
			preview.Show(this.printDocument, ownerForm);
		}

		public override void Print()
		{
			try
			{
				this.printDocument.Print();
			}
			catch (Exception exc)
			{
				MessageBox.Show(exc.Message, NorthwindApp.MessageCaption, MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		public override bool Printable
		{
			get
			{
				return true;
			}
		}

		private void UpdateLayout()
		{
			if (this.Timeline.CurrentLayout != null)
			{
				//first clear all valuelist to avoid saving them in the layout
				//file. ValueList in this app, are filled when the control is loaded
				//so there is no need to preserve them
				foreach (TimeLineField f in this.Timeline.Fields)
				{
					if (f.HasValueList)
					{
						f.ValueList.Clear();
					}
				}
				this.Timeline.CurrentLayout.Update();
			}
		}
		protected override void OnFolderViewChanged()
		{
			UpdateLayout();
		}
		public override void SaveSettings()
		{
			UpdateLayout();

			ConfigDataSet.ContentViewSettingsRow viewRow = NorthwindApp.ConfigDataSet.ContentViewSettings.FindByViewName(this.GetType().FullName);

			if (viewRow == null)
			{
				viewRow = NorthwindApp.ConfigDataSet.ContentViewSettings.AddContentViewSettingsRow(this.GetType().FullName, "");
			}
			viewRow.BeginEdit();
			System.IO.MemoryStream stream = new System.IO.MemoryStream();
			this.Timeline.SaveLayoutFile(stream);
			viewRow.Layout = System.Convert.ToBase64String(stream.ToArray());
			stream.Close();
			viewRow.EndEdit();
		}
		public override void LoadSettings()
		{
			//Load saved layout if the application is reading layouts and 
			//if it exist in the config dataset

			if (NorthwindApp.ReadSettingsFromDS)
			{
				ConfigDataSet.ContentViewSettingsRow viewRow = NorthwindApp.ConfigDataSet.ContentViewSettings.FindByViewName(this.GetType().FullName);

				if (viewRow != null)
				{
					if (! viewRow.IsLayoutNull() && viewRow.Layout.Length > 0)
					{
						System.IO.MemoryStream stream = new System.IO.MemoryStream(System.Convert.FromBase64String(viewRow.Layout));
						this.Timeline.LoadLayoutFile(stream);
						stream.Close();
					}
				}
			}
		}


		public override bool CanEdit
		{
			get
			{
				foreach (ITimeLineObject selectedItem in this.Timeline.SelectedItems)
				{
					if (selectedItem.ItemType == ItemType.TimeLineItem)
					{
						return true;
					}
				}
				return false;
			}
		}

		private void Timeline_SelectedItemsChanged(object sender, System.EventArgs e)
		{
			this.OnCurrentItemChanged(EventArgs.Empty);
		}

		private void Timeline_DoubleClick(object sender, System.EventArgs e)
		{
			if (this.Timeline.HitTest() == TimeLineArea.TimeLineItem)
			{
				this.Edit();
			}
		}

        private void Ribbon1_CommandClick(object sender, Janus.Windows.Ribbon.CommandEventArgs e)
        {
            switch (e.Command.Key)
            {
                case "rcmdGroupBy":
                    this.ShowGroupByDialog();
                    break;
                case "rcmdConditionalFormatting":
                    this.ShowFormattingDialog();
                    break;
                case "rcmdTimescale":
                    this.ShowTimescaleDialog();
                    break;
                case "rcmdFilterPanel":
                    this.FilterEditor1.Visible = this.rcmdFilterPanel.Checked;
                    break;
            }
        }


	}

} //end of root namespace